﻿Imports ADOEntity

Partial Class Administrator_BranchEdit
    Inherits Web.UI.BasePage
    Dim sBranchEntity As userEntities
    Dim clsMasterDropDown As clsMasterDropDown

    Dim strStatus As String
    Dim strMsg As String = ""
#Region " Even Page"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblBranchCode.Text = Resources.resBranch.branchCode
        lblBranchName.Text = Resources.resBranch.branchName
        lblDept.Text = Resources.resBranch.department
        lblZone.Text = Resources.resBranch.Zone

        btnOK.Text = Resources.resButton.btnSave
        btnCancel.Text = Resources.resButton.btnCancel

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            chkIsLogin()
            strStatus = Request.QueryString("Status")
            Dim strBranchCode As String = Request.QueryString("strBranchCode")
            clsMasterDropDown = New clsMasterDropDown
            sBranchEntity = New UserEntities

            If Not (IsPostBack) Then


                If (strStatus = "Add") Then
                    loadDataToDropDown()
                ElseIf (strStatus = "Edit") Then
                    Dim qry = (From Bran In sBranchEntity.SBRANCH Where Bran.BRANCHCODE = strBranchCode).ToList
                    txtBranchCode.Text = qry(0).BRANCHCODE
                    txtBranchName.Text = qry(0).BRANCHNAME
                    loadDataToDropDown(qry(0).DEPTCODE, qry(0).ZONECODE)
                    ddlDepartment.SelectedValue = qry(0).DEPTCODE

                End If

            End If
            clsMasterDropDown = Nothing
            sBranchEntity = Nothing

            ChkPageStatus(strStatus)
        Catch ex As Exception

        End Try
    End Sub
#End Region

#Region " Even Button "
    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim chkPassSave As Int16 = 0
        sBranchEntity = New userEntities
        Try
            If chkBeforeSave() Then
                Dim sBranch As New SBRANCH
                If (strStatus = "Add") Then

                    sBranch.BRANCHCODE = txtBranchCode.Text
                    sBranch.BRANCHCODEUSE = txtBranchCode.Text.Substring(0, 3)
                    sBranch.BRANCHNAME = txtBranchName.Text
                    sBranch.DEPTCODE = ddlDepartment.SelectedValue
                    sBranch.ZONECODE = ddlZone.SelectedValue
                    sBranch.ALLCODERIGHT = ddlDepartment.SelectedValue & "/" & txtBranchCode.Text.Trim & "/" & ddlZone.SelectedValue
                    sBranchEntity.AddToSBRANCH(sBranch)
                    sBranchEntity.SaveChanges()
                    chkPassSave = 1

                ElseIf (strStatus = "Edit") Then

                    Dim qry = (From Branch In sBranchEntity.SBRANCH Where Branch.BRANCHCODE = txtBranchCode.Text).ToList
                    qry(0).BRANCHNAME = txtBranchName.Text
                    qry(0).DEPTCODE = ddlDepartment.SelectedValue
                    qry(0).ZONECODE = ddlZone.SelectedValue
                    qry(0).ALLCODERIGHT = ddlDepartment.SelectedValue & "/" & txtBranchCode.Text.Trim & "/" & ddlZone.SelectedValue
                    sBranchEntity.SaveChanges()
                    chkPassSave = 1
                End If
            Else
                ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + strMsg + "')", True)
            End If
        Catch ex As System.Data.UpdateException
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" + ex.InnerException.Message + "')", True)
            chkPassSave = 0
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('บันทึกผิดพลาด')", True)
            chkPassSave = 0

        End Try

        If (chkPassSave = 1) Then
            Response.Redirect("BranchList.aspx")
        End If

    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("BranchList.aspx")
    End Sub
#End Region

#Region " Function "

    Private Sub loadDataToDropDown(Optional ByVal strDeptCode As String = "", Optional ByVal strZoneCode As String = "")
        clsMasterDropDown.setDeptToDdl(ddlDepartment, selectData:=strDeptCode)
        clsMasterDropDown.setZoneToDdl(ddlZone, selectData:=strZoneCode)
    End Sub

    Private Sub ChkPageStatus(ByVal strStatus As String)
        If Not (strStatus = "Add") Then
            txtBranchCode.Enabled = False
        End If
    End Sub

    Private Function chkBeforeSave() As Boolean
        Dim result As Boolean = True


        If (strStatus = "Add") Then
            If (txtBranchCode.Text = "") Then
                result = False
                strMsg = strMsg & lblBranchCode.Text & " : " & "กรุณาระบุรหัสสาขา/ส่วนงาน" & "\n"
            Else
                Dim EmpCount As Integer = (From branch In sBranchEntity.SBRANCH Where branch.BRANCHCODE = txtBranchCode.Text).Count
                If Not (EmpCount = 0) Then
                    result = False
                    strMsg = strMsg & lblBranchCode.Text & " : " & "มีรหัสสาขา/ส่วนงานนี้อยู่ในระบบแล้ว" & "\n"
                End If
            End If

        End If


        If (txtBranchName.Text = "") Then
            result = False
            strMsg = strMsg & lblBranchName.Text & " : " & "กรุณาระบุชื่อสาขา/ส่วนงาน" & "\n"

        End If

        If (ddlDepartment.SelectedValue = "0") Then
            result = False
            strMsg = strMsg & lblDept.Text & " : " & "กรุณาระบุฝ่าย" & "\n"

        End If

        If (ddlZone.SelectedValue = "0") Then
            result = False
            strMsg = strMsg & lblZone.Text & " : " & "กรุณาระบุประเภท" & "\n"

        End If

        Return result

    End Function
#End Region

End Class
